package org.wang.portal.task;

import org.wang.portal.alert.AlertManager;
import org.wang.portal.param.PreMigrationValidator;
import org.wang.portal.status.TaskStatus;

public class MigrationTaskExecutor {
    private MigrationTask task;
    private AlertManager alertManager;

    public MigrationTaskExecutor(MigrationTask task) {
        this.task = task;
        this.alertManager = new AlertManager();
    }

    public void execute() {
        // 准备执行环境 ,包括工作目录，参数校验，配置文件写入
        task.prepareExecuteEnv();


        // 启动任务
        task.execute();

        // 任务状态监听
        while (task.getStatus() != TaskStatus.COMPLETED && task.getStatus() != TaskStatus.FAILED) {
            System.out.println("Task " + task.getName() + " is in progress. Progress: " + task.getProgress() + "%");
            try {
                Thread.sleep(1000); // 每秒检查一次任务状态
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 任务告警监听
        if (task.getStatus() == TaskStatus.FAILED) {
            alertManager.sendAlert(task);
        }

        System.out.println("Task " + task.getName() + " completed with status: " + task.getStatus());
    }


}
